const app = getApp();
const {upTime} = require('/util/public');
Page({
  data: {
    calendarList: [],
    unfoldSta: false,
    thisYear: '0000',
    thisMonth: '0',
    calendarHeight: '150',
    startTimerNum: 0,
    endTimerNum: 0,

    stayList:[]
  },


  onLoad() {
    this.resetBar();
    this.getThisMonth();
  },

  // 重置颜色
  resetBar(){
    my.setNavigationBar({
      reset: true
    });
  },

  navigateTo(e){
    app.navigateTo(e);
  },

  // 获取当前月份
  getThisMonth(){
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();

    let thisMonthDay = new Date(year, month, 0).getDate();
    let frontMonthDay = new Date(year, month-1, 0).getDate();
    let nextMonthDay = new Date(year, month+1, 0).getDate();
    let weekNum = new Date(year, month,0).getDay();
    let nextNum = 6 - weekNum;
    let frontNum = 42 - thisMonthDay - nextNum;

    let arr = [];
    for(let i=1; i<=frontNum; i++){
      let obj = {
        day: frontMonthDay-(frontNum-i),
        month: month==1?12:month-1,
        thisSta: false,
        waitSta: false
      }
      arr.push(obj);
    }
    for(let i=1; i<=thisMonthDay; i++){
      let obj = {
        day: i,
        month: month,
        thisSta: false,
        waitSta: false
      }
      if(i == day){
        obj.thisSta = true
      }
      arr.push(obj);
    }
    for(let i=1; i<=nextNum; i++){
      let obj = {
        day: i,
        month: month==12?1:month+1,
        thisSta: false,
        waitSta: false
      }
      arr.push(obj);
    }

    let start = year +'/'+ arr[0].month + '/' + arr[0].day + ' ' + '00:00:00';
    let end = year +'/'+ arr[arr.length-1].month + '/' + arr[arr.length-1].day + ' ' + '00:00:00';

    
    let startTimer = new Date(start);
    let startTimerNum = startTimer.getTime()/1000 + '';
    // let endTimer = new Date("2022-08-31 00:00:00");
    let endTimer = new Date(end);
    let endTimerNum = endTimer.getTime()/1000 + '';

    console.log(start,end,startTimerNum,endTimerNum);
    
    this.setData({
      calendarList: arr,
      thisYear: year,
      thisMonth: month,
      startTimerNum: startTimerNum,
      endTimerNum: endTimerNum
    })

    // 获取高度
    this.getCalendarHeight();

    this.getList(startTimerNum,endTimerNum);
  },


  // 获取列表
  getList(startTime,endTime){
    let data = {
      startPeriodStartDate: startTime,
      startPeriodEndDate: endTime,
      // uid:9292
    }
    app.order.monthList(data)
    .then(res=>{
      console.log(666,res);
      if(res.code == 200 && res.data){

        this.getGoodsData(res.data)

        let arr = this.data.calendarList

        res.data.forEach(item => {
          let timer = (item.exptimerArr[0]-0)+'-'+(item.exptimerArr[1]-0);
          arr.forEach(ite=>{
            let tim = ite.month + '-' + ite.day;
            if(tim == timer){
              ite.waitSta = true;
            }
          })
        });

        this.setData({
          stayList: res.data,
          calendarList: arr
        })

        console.log(888,this.data.stayList)
      }
    })
  },

  // 转换商品信息格式
  getGoodsData(params){
    return new Promise((resole,reject)=>{
        for (let i = 0;i<params.length;i++){
          let newdat = JSON.parse(params[i].productParams);
          params[i].goodsData = newdat;
          let exptim = upTime("%Y/%M/%D",params[i].expiretime);
          let exptimerArr = [];
          exptimerArr.push(exptim.split('/')[1]-0,exptim.split('/')[2]-0);
          params[i].exptimerArr = exptimerArr;
        }
        resole(params)
    })
},

  // 点击展开
  unfoldBtn(){
    this.setData({
      unfoldSta: !this.data.unfoldSta
    })
    this.getCalendarHeight();
  },


  // 获取日期表高度
  getCalendarHeight(){
    let that = this;
    my.createSelectorQuery()
      .select('#topId').boundingClientRect()
      .exec((ret) => {
        let hei = Math.round(ret[0].height);
        that.setData({
          calendarHeight: hei
        })
        console.log(hei)
    })
  }
  







});
